package org.groupg.practice.collections;

import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.HashBag;

public class BagExample {

    public static void main(String[] args) {
        // 创建一个 Bag 实例
        Bag<String> bag = new HashBag<>();

        // 添加元素
        bag.add("apple", 3); // 添加3个apple
        bag.add("banana", 2); // 添加2个banana
        bag.add("apple");
        // 获取元素出现次数
        int appleCount = bag.getCount("apple");
        System.out.println("Apple count: " + appleCount); // 输出: 3

        // 移除元素
        bag.remove("apple", 1);
        System.out.println("Apple count after removal: " + bag.getCount("apple")); // 输出: 2

        // 获取所有不重复元素
        System.out.println("Unique elements: " + bag.uniqueSet()); // 输出: [apple, banana]
    }
}
